export default async function getBtoaWithFetch(imageUrl) {
  try {
    const blob = await fetch(imageUrl).then((res) => res.blob());
    return await new Promise((resolve, reject) => {
      const fileReader = new FileReader();
      fileReader.onloadend = () => {
        resolve(fileReader.result);
      };
      fileReader.onerror = () => {
        reject("Convert failed");
      };
      fileReader.readAsDataURL(blob);
    });
  } catch (error) {
    const typeError = new TypeError();
    typeError.data = {
      code: 0,
      message: "Failed",
      data: {
        error,
      },
    };
    return Promise.reject(typeError);
  }
}

// getBtoaWithFetch(
//   "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3636058960,1338604857&fm=26&gp=0.jpg"
// )
//   .then((ret) => {
//     console.log(ret);
//   })
//   .catch((err) => {
//     console.log(err.data);
//   });
